#include "PortalSubject.h"

#include <algorithm>

#include "Conversions.h"
#include "Logger.h"
#include "Portal.h"
#include "PortalListener.h"

PortalSubject::~PortalSubject()
{
}

PortalSubject& PortalSubject::operator=(const PortalSubject& rhs)
{
    if(this == &rhs) return *this;

    listeners = rhs.listeners;

    return *this;
}

void PortalSubject::registerListener(PortalListener* listener)
{
    if(std::find(listeners.begin(), listeners.end(), listener) == listeners.end())
    {
        listeners.push_back(listener);
    }
}

void PortalSubject::unregisterListener(PortalListener* listener)
{
    listener_it it = std::find(listeners.begin(), listeners.end(), listener);
    if(it != listeners.end())
    {
        listeners.erase(it);
    }
    else
    {
        using namespace Conversions;
        LOG("Warning: portal listener at " + toString(listener) +
            " already registered with portal subject at " + toString(this) + '.');
    }
}

void PortalSubject::notifyListeners(const Portal& hit)
{
    for(listener_it it = listeners.begin(); it != listeners.end(); ++it)
    {
        (*it)->notifyPortalHit(hit);
    }
}